﻿using System;
using System.Collections.Generic;
using System.Text;
using Wjtour.Common;
using Wjtour.Domain;
using System.Linq;

namespace Wjtour.BLL
{
    public class uploadfile_bll : base_dll
    {
        public PaginationData<Wjtour.Domain.uploadfile> GetList(string q, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(q))
                where += " and nick like '%" + Utility.GetSafeSql(q) + "%' ";
            string sql = string.Format(@"select * from uploadfile 
                {0} 
                order by id desc", where);
            PaginationData<Wjtour.Domain.uploadfile> data = GetPaginationData<Wjtour.Domain.uploadfile>(sql, page, pagesize);
            return data;
        }

        public Wjtour.Domain.uploadfile Load(int id)
        {
            string sql = "select * from uploadfile where id=" + id;
            return DbAccess.GetObjects<Wjtour.Domain.uploadfile>(sql).FirstOrDefault();
        }

        public bool Save(Wjtour.Domain.uploadfile uploadfile)
        {
            string sql = "";
            if (uploadfile.id > 0)
            {
                sql = string.Format(@"update uploadfile 
                set title='{0}',url='{1}',img='{2}',type={3},sort={4}
                where id={5}",
                             uploadfile.id);
            }
            else
                sql = string.Format(@"insert into uploadfile(nick) values('{0}')", uploadfile.id);

            return DbAccess.ExecuteNonQuery(sql) > 0;
        }

        public int BatchDelete(string ids)
        {
            string sql = "delete from uploadfile where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }
    }
}
